/***********************************************************************************************//**
*\n  @file       app_dna.c
*\n  @brief      DNA模块,主文件
*\n  @details
*\n -----------------------------------------------------------------------------------
*\n  文件说明：
*\n       1. DNA模块, 启动接口
*\n
*\n -----------------------------------------------------------------------------------
*\n  版本:    修改人:       修改日期:          描述:
*\n  V0.01    罗先能        2021.06.10      创建
*\n
***************************************************************************************************/

/**************************************************************************************************
* 头文件
***************************************************************************************************/
#include "hal.h"
#include "framework.h"

#include "app_dna.h"
#include "MTRT_SNInfoAPI.h"

#undef   DEBUG_LEVEL
#define  DEBUG_LEVEL  5
#include "vdebug.h"

/**************************************************************************************************
* 宏定义、结构定义
***************************************************************************************************/


/**************************************************************************************************
* 全局变量声明、定义
***************************************************************************************************/
TDnaHandler  tDnaHander1 =
{
	.pid = 0,
	.keyl = 0,
	.keyh = 0,
	.dnal = 0,
	.dnah = 0,
	.resalt = 0,
};



/**************************************************************************************************
* 私有变量声明、定义
***************************************************************************************************/


/**************************************************************************************************
* 私有函数声明、定义
***************************************************************************************************/
static void usage(char *name )
{
	printf("--------- DNA Test ---------------------\n");
	printf("Usage:\n");
	printf("download fpga: %s load <filename> \n", name);
	printf("read DNA:      %s rd  \n", name);
	printf("check DNA:     %s check <sn_file> \n", name);
	printf(" <filename>  - download fpga bitstream file\n");
	printf("\n");
}

/**************************************************************************************************
* 全局函数声明、定义
***************************************************************************************************/


// 启动app DNA模块
int AppDnaStart(int argc, char *argv[])
{
	int ret;

	if (argc < 2)
	{
		usage(argv[0]);
		return -1;
	}
	else if (strcmp(argv[1],  "rd") == 0)  // 读取 ---------------------
	{
		u32 dnal = 0;
		u32 dnah = 0;
		HwOpen(DEV_NO_DNA, NULL);
		ret = HwRead(DEV_NO_DNA, 14*4, &dnal, sizeof(dnal));
		ret = HwRead(DEV_NO_DNA, 15*4, &dnah, sizeof(dnah));
		if (ret < 0)
		{
			debug_error("HwRead() < 0\n");
			HwClose(DEV_NO_DNA, NULL);
			return -1;
		}
		tDnaHander1.dnal = dnal;
		tDnaHander1.dnah = dnah;

		u64  value;
		value  = dnah;
		value  = (value << 32) + dnal;
		printf("DNA=0x%08x_%08x, %llu \n", dnah, dnal, value);
		return 0;
	}
	else if (strcmp(argv[1],  "load") == 0)  // 下载fpga --------------------
	{
		if (argc != 3)
		{
			usage(argv[0]);
			return -1;
		}
		ret = HwOpen(DEV_NO_DNA, NULL);
		ret = HwLoad(DEV_NO_DNA, argv[2]);
		if (ret < 0)
		{
			debug_error("HwLoad(DEV_NO_DNA) < 0\n");
			HwClose(DEV_NO_DNA, NULL);
			return -1;
		}
		printf("load %s ok \n", argv[2]);
		HwClose(DEV_NO_DNA, NULL);
		return 0;
	}
	else if (strcmp(argv[1],  "check") == 0)  // 验证DNA ----------------
	{
		SNLineInfo  tInfo = {0};

		if (argc != 3)
		{
			usage(argv[0]);
			return -1;
		}

		ret = access(argv[2], 0);   // 检测文件存在?
		if (ret != 0)
		{
			usage(argv[0]);
			return -1;
		}

		tInfo = MTReadSNDat(argv[2]);
		printf("tInfo.FpgaDNA: %llu \n", tInfo.FPGADNA);
		printf("tInfo.FpgaSN:  %llu \n",  tInfo.FPGASN);
		printf("tInfo.ProductID: %u \n",  tInfo.ProductID);
		printf("tInfo.TargetType: %u \n",  tInfo.TargetType);

		tDnaHander1.pid = tInfo.ProductID;
		tDnaHander1.keyh = (u32)(tInfo.FPGASN >> 32);
		tDnaHander1.keyl = (u32)tInfo.FPGASN;

		ret = HwOpen(DEV_NO_DNA, NULL);
		ret = HwRead(DEV_NO_DNA, 14*4, &tDnaHander1.dnal, sizeof(tDnaHander1.dnal));  // 读取DNA
		ret = HwRead(DEV_NO_DNA, 15*4, &tDnaHander1.dnah, sizeof(tDnaHander1.dnah));
		u64  dna;
		dna = tDnaHander1.dnah;
		dna = (dna << 32) + tDnaHander1.dnal;
		printf("Fpga DNA: %llu \n",  dna);
		printf("tDnaHander1.pid: %u \n",  tDnaHander1.pid);
		printf("tDnaHander1.keyl: %u \n",  tDnaHander1.keyl);
		printf("tDnaHander1.keyh: %u \n",  tDnaHander1.keyh);

		ret  = HwWrite(DEV_NO_DNA, (16+7)*4, &tDnaHander1.pid,  4);
		ret += HwWrite(DEV_NO_DNA, (16+14)*4, &tDnaHander1.keyl, 4);
		ret += HwWrite(DEV_NO_DNA, (16+15)*4, &tDnaHander1.keyh, 4);
		if (ret < 0)
		{
			debug_error("HwWrite(DEV_NO_DNA) < 0\n");
			HwClose(DEV_NO_DNA, NULL);
			return -1;
		}
		sleep(5);
		ret = HwRead(DEV_NO_DNA, 13*4, &tDnaHander1.resalt, 4);
		if (ret < 0)
		{
			debug_error("HwRead(DEV_NO_DNA) < 0\n");
			HwClose(DEV_NO_DNA, NULL);
			return -1;
		}
		HwClose(DEV_NO_DNA, NULL);
		printf("DNA calculate == %d \n", tDnaHander1.resalt);
		// return 0;
	}
	else
	{
		usage(argv[0]);
		return -1;
	}

  	return 0;
}

